<?php

namespace App\Http\Controllers\Admin;

use App\Models\Tag;
use App\Repositories\TagRepository;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class TagController extends Controller
{
    protected $tag;

    public function __construct(TagRepository $tag)
    {
        $this->tag = $tag;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $extra['level']=1;
        $size = $request->get('size')?$request->get('size'):10;
        $tags = $this->tag->index([],$extra,$size);
        return view('admin.back.tag.index', compact('tags'));
    }

    public function create(Request $request)
    {
        $pid = $request->get('pid');
        return view('admin.back.tag.create',compact('pid'));
    }
    public function createClass()
    {
        return view('admin.back.tag.createClass');
    }

    public function store(Requests\TagClassRequest $request)
    {
        $this->tag->store($request);
        $pid = $request->get('pid');
        if($pid){
            return redirect()->to(url('/admin/tag/manage', ['id'=>$pid]))->with('message', '成功新增子标签！');
        }
        return redirect()->to(site_path('tag', 'admin'))->with('message', '成功新增标签分类！');
    }

    public function show($id)
    {
        //
    }
    public function edit($id)
    {
        $tag = $this->tag->edit($id);
        return view('admin.back.tag.edit', compact('tag'));
    }
    public function update(Requests\TagClassRequest $request, $id)
    {
        $this->tag->store($request);
        return redirect()->to(site_path('tag', 'admin'))->with('message', '成功修改标签分类！');
    }

    public function destroy($id)
    {
        $this->tag->destroy($id);
        return redirect()->to(site_path('tag', 'admin'))->with('message', '成功删除分类！');
    }
    public function destroyOne($id)
    {
        $this->tag->destroy($id);
        return redirect()->to(site_path('tag', 'admin'))->with('message', '成功删除分类！');
    }

    public function manage(Request$request,$id)
    {
        $class = $this->tag->edit($id);
        $tree = $this->tag->getTree($id);
        $tags = $this->tag->treeSort($tree);
        return view('admin.back.tag.manage',compact('tags','class'));
    }
}
